import { _decorator, Button, color, Component, ImageAsset, Label, Node, Sprite, SpriteFrame, Texture2D } from 'cc';
import { HomeViewModel } from '../HomeViewModel';
import { UIComponent } from '../../../../platform/ui/UIComponent';
import { Menu_Btns } from '../../../common/enums';
import { ResManager } from 'db://assets/scripts/platform/resources/ResMgr';
import { bind } from 'db://assets/scripts/platform/ui/UIDecorator';
import { LogMgr } from 'db://assets/scripts/platform/log/LogMgr';
const { ccclass, property } = _decorator;

@ccclass('MenuBtn')
export class MenuBtn extends UIComponent {

    @bind(Sprite)
    icon:Sprite
    @bind(Label)
    menu_name:Label

    btn_name:number
    public init(btn_name:number){
        this.btn_name = btn_name;
        this.initData()
        this.node.on(Button.EventType.CLICK,this.select,this)
        this.updateStatus()
    }
    initData() {
        let path = ""
        let name = ""
        if(this.btn_name==Menu_Btns.All){
            path="resources/texture/common/menu_all"
            name = "全部"
        }
        if(this.btn_name==Menu_Btns.Played){
            path="resources/texture/common/played_default"
            name = "玩过"
        }
        if(this.btn_name==Menu_Btns.Mall){
            path="resources/texture/common/played_default"
            name = "商城"
        }
        if(this.btn_name==Menu_Btns.Achivement){
            path="resources/texture/common/played_default"
            name = "成就"
        }
        if(this.btn_name==Menu_Btns.My){
            path="resources/texture/common/played_default"
            name = "我的"
        }
        ResManager.inst.loadSpriteFrame(path).then((spriteFrame)=>{
            this.icon.spriteFrame = spriteFrame;
        })
        this.menu_name.string =name
    }
    updateStatus() {
        let model =this.ownerView.dataModel as HomeViewModel;
        if(model.selectMenu.value===this.btn_name){
            this.menu_name.color = color(0,255,0)
            this.icon.color = color(0,255,0)
        }else{
            this.menu_name.color = color(0,0,0)
            this.icon.color = color(0,0,0)
        }
    }

    //设置被选中
    public select(){
        LogMgr.info("选中菜单按钮"+this.btn_name)
        this.ownerView.dataModel.selectMenu.value = this.btn_name;
    }
}


